package com.qtz.demo.controller;


import com.qtz.demo.bean.Answer;
import com.qtz.demo.service.AnswerSereviceImpl;
import com.qtz.demo.service.QuestionServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/answer")
public class AnswerController {

    @Autowired
    private AnswerSereviceImpl answerSerevice;
    @Autowired
    private QuestionServiceImpl questionService;

    @RequestMapping(value = "/addAnswer",method = RequestMethod.POST)
    @ResponseBody
    public boolean addAnswer(@RequestBody Map<String,String> map){

        String current = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format( new Date());
        Timestamp time = Timestamp.valueOf(current);

        Answer answer = new Answer();
        answer.setQid(map.get("qid"));
        answer.setUid(map.get("uid"));
        answer.setAnswer(map.get("answer"));
        answer.setCreatetime(time);

        if(answerSerevice.addAnswer(answer)){
            return questionService.dealQuestion(map.get("qid"));
        }
        return false;
    }

}
